package cn.itcast.bos.service.base.impl;

import cn.itcast.bos.dao.base.CourierRepository;
import cn.itcast.bos.dao.base.FixAreaRepository;
import cn.itcast.bos.dao.base.TakeTimeRepository;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.TakeTime;
import cn.itcast.bos.service.base.FixedAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class FixedAreaServiceImpl implements FixedAreaService{
    @Autowired
    private FixAreaRepository fixAreaRepository;

    @Override
    public void save(FixedArea model) {
        fixAreaRepository.save(model);
    }

    @Override
    public Page<FixedArea> findPageData(Specification<FixedArea> specification,Pageable pageable) {
        Page<FixedArea> all = fixAreaRepository.findAll(specification,pageable);
        return all;
    }

    @Autowired
    private CourierRepository courierRepository;
    @Autowired
    private TakeTimeRepository takeTimeRepository;

    //快递员关联定区
    @Override
    public void associationCourierToFixedArea(FixedArea model, Integer courierId, Integer takeTimeId) {
        FixedArea fixedArea = fixAreaRepository.findOne(model.getId());
        //查询出快递员
        Courier courier = courierRepository.findOne(courierId);
        //查询出排班对象
        TakeTime time = takeTimeRepository.findOne(takeTimeId);
        //快递员关联排班时间
        courier.setTakeTime(time);
        //定区添加快递员
        fixedArea.getCouriers().add(courier);
//        fixAreaRepository.save(fixedArea);

    }

    @Override
    public FixedArea findOne(String fixedAreaId) {
        FixedArea one = fixAreaRepository.findOne(fixedAreaId);
        return one;
    }


}
